﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSpotLib.Crypto;
using Org.Mentalis.Security.Cryptography;
using System.IO;

namespace SharpSpotLib.Util
{
    /// <summary>
    /// This class provides methods to intercept and change random data to known values.
    /// </summary>
    internal static class DebugAdapter
    {
        private static MemoryStream _ms;

        public static Byte[] SetClientRandom()
        {
            return new Byte[] { 117, 143, 140, 15, 157, 229, 137, 106, 28, 116, 8, 220, 60, 92, 216, 2 };

        }

        public static DHKeyPair SetDHClientKeyPair()
        {
            Byte[] privateX = new Byte[] { 40, 254, 41, 26, 111, 31, 140, 105, 199, 121, 64, 184, 247, 75, 206, 255, 149, 14, 
                98, 32, 254, 29, 253, 187, 75, 89, 0, 239, 238, 5, 132, 149, 178, 177, 163, 219, 203, 73, 79, 57, 238, 
                93, 101, 75, 52, 82, 82, 10, 66, 116, 59, 198, 129, 29, 86, 75, 40, 228, 19, 171, 56, 152, 245, 144, 
                74, 19, 90, 83, 234, 232, 81, 79, 43, 122, 55, 76, 244, 64, 20, 135, 151, 165, 44, 23, 105, 46, 93, 2, 
                75, 13, 61, 121, 178, 127, 187, 178 };
            DiffieHellman dh = new DiffieHellmanManaged(DH.P, DH.G, privateX);
            DHParameters privateKey = dh.ExportParameters(true);
            return new DHKeyPair(
                new DHPrivateKey(privateKey),
                new DHPublicKey(dh.CreateKeyExchange()));
        }

        public static RSAKeyPair SetRSAClientKeyPair()
        {
            Byte[] priv = new Byte[] { 185, 174, 120, 53, 109, 246, 254, 97, 197, 86, 11, 255, 65, 249, 14, 3, 64, 243, 234, 
                201, 173, 109, 163, 88, 56, 186, 20, 196, 156, 194, 219, 121, 31, 75, 206, 137, 105, 130, 
                83, 137, 139, 208, 169, 149, 142, 118, 108, 133, 70, 252, 94, 4, 46, 90, 181, 242, 144, 192, 110, 
                240, 173, 181, 255, 115, 98, 245, 12, 6, 191, 156, 20, 236, 63, 210, 0, 29, 220, 112, 131, 234, 166, 
                161, 245, 22, 33, 171, 197, 165, 77, 118, 234, 75, 239, 218, 238, 231, 136, 8, 213, 30, 210, 231, 
                191, 77, 82, 31, 119, 238, 13, 108, 104, 228, 42, 81, 79, 32, 137, 6, 41, 60, 87, 136, 24, 216, 234, 
                18, 197, 235 };
            Byte[] publ = new Byte[] { 185, 174, 120, 53, 109, 246, 254, 97, 197, 86, 11, 255, 65, 249, 14, 3, 64, 243, 234, 
                201, 173, 109, 163, 88, 56, 186, 20, 196, 156, 194, 219, 121, 31, 75, 206, 137, 105, 130, 83, 137, 
                139, 208, 169, 149, 142, 118, 108, 133, 70, 252, 94, 4, 46, 90, 181, 242, 144, 192, 110, 240, 173, 
                181, 255, 115, 98, 245, 12, 6, 191, 156, 20, 236, 63, 210, 0, 29, 220, 112, 131, 234, 166, 161, 
                245, 22, 33, 171, 197, 165, 77, 118, 234, 75, 239, 218, 238, 231, 136, 8, 213, 30, 210, 231, 191, 
                77, 82, 31, 119, 238, 13, 108, 104, 228, 42, 81, 79, 32, 137, 6, 41, 60, 87, 136, 24, 216, 234, 18, 
                197, 235 };
            return new RSAKeyPair(new RSAPrivateKey(priv), new RSAPublicKey(publ));
        }

        public static Int32 ReceiveData(Byte[] buffer)
        {
            return _ms.Read(buffer, 0, buffer.Length);
        }

        public static Int32 ReceiveData(Byte[] buffer, Int32 offset, Int32 length)
        {
            return _ms.Read(buffer, offset, length);
        }

        public static void SendData(Byte[] buffer)
        {
            byte[] exp1 = new Byte[] { 0, 3, 1, 29, 0, 0, 0, 0, 0, 3, 12, 0, 0, 1, 134, 158, 0, 0, 0, 0, 1, 0, 0, 0, 1, 4, 1, 1, 0, 0, 0, 0, 117, 143, 140, 15, 157, 229, 137, 106, 28, 116, 8, 220, 60, 92, 216, 2, 194, 222, 195, 48, 97, 198, 162, 250, 194, 25, 31, 100, 235, 8, 18, 15, 111, 76, 95, 250, 87, 231, 252, 54, 239, 146, 154, 121, 147, 101, 157, 215, 60, 30, 191, 110, 195, 119, 119, 133, 83, 140, 118, 70, 38, 141, 202, 42, 107, 6, 121, 65, 164, 12, 115, 133, 46, 87, 55, 44, 245, 78, 221, 82, 220, 156, 228, 179, 192, 44, 17, 253, 241, 6, 91, 173, 2, 191, 146, 147, 227, 136, 32, 90, 55, 85, 151, 242, 177, 146, 170, 149, 56, 250, 203, 114, 185, 174, 120, 53, 109, 246, 254, 97, 197, 86, 11, 255, 65, 249, 14, 3, 64, 243, 234, 201, 173, 109, 163, 88, 56, 186, 20, 196, 156, 194, 219, 121, 31, 75, 206, 137, 105, 130, 83, 137, 139, 208, 169, 149, 142, 118, 108, 133, 70, 252, 94, 4, 46, 90, 181, 242, 144, 192, 110, 240, 173, 181, 255, 115, 98, 245, 12, 6, 191, 156, 20, 236, 63, 210, 0, 29, 220, 112, 131, 234, 166, 161, 245, 22, 33, 171, 197, 165, 77, 118, 234, 75, 239, 218, 238, 231, 136, 8, 213, 30, 210, 231, 191, 77, 82, 31, 119, 238, 13, 108, 104, 228, 42, 81, 79, 32, 137, 6, 41, 60, 87, 136, 24, 216, 234, 18, 197, 235, 0, 8, 1, 0, 106, 97, 99, 107, 106, 115, 111, 110, 64 };

            //Set correct return data
            if (buffer.Length == 285 && buffer.SequenceEqual(exp1)) //FIXME
            {
                _ms = new MemoryStream(rawData1);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        private static byte[] rawData1 = {
	        0x00, 0xDB, 0xC4, 0xB8, 0xA4, 0xC8, 0xC8, 0xC7, 0xAE, 0x03, 0x02, 0xD2,
	        0xEF, 0xDE, 0xCE, 0x81, 0xA3, 0xCD, 0x67, 0x8F, 0xC5, 0x5F, 0x4D, 0x99,
	        0x55, 0x09, 0xEF, 0x49, 0x60, 0xE9, 0xA6, 0x2B, 0xF8, 0x1A, 0x0A, 0x2C,
	        0xCE, 0xEA, 0xCE, 0xE1, 0xD2, 0xFB, 0x29, 0x05, 0xD1, 0xF3, 0x50, 0x84,
	        0x3A, 0x7F, 0x83, 0x37, 0xC6, 0x46, 0x41, 0x29, 0xC4, 0x39, 0xC5, 0x45,
	        0x0B, 0xF5, 0x62, 0xBD, 0xDF, 0x59, 0xAC, 0xFC, 0x01, 0x8C, 0xCC, 0xC8,
	        0x64, 0xB6, 0xA1, 0xDB, 0x49, 0x27, 0xC3, 0x82, 0x97, 0x57, 0x9E, 0xD7,
	        0xA6, 0xD4, 0x77, 0x92, 0xFB, 0xFF, 0x84, 0x10, 0x6C, 0x98, 0x37, 0x3C,
	        0x6D, 0x35, 0x67, 0xF4, 0x12, 0xF9, 0xB4, 0x88, 0x16, 0x0C, 0xDA, 0xE2,
	        0x54, 0x4D, 0xA9, 0x28, 0x73, 0x93, 0x4D, 0x46, 0x4D, 0x7E, 0x9D, 0xC0,
	        0xAB, 0xC2, 0x91, 0xC7, 0x1F, 0xEB, 0x65, 0xE3, 0xF1, 0x55, 0xDA, 0xAF,
	        0x10, 0xDF, 0x37, 0xF8, 0x8B, 0x59, 0x36, 0xA5, 0x43, 0xB8, 0xD3, 0xEB,
	        0xA5, 0x90, 0xB6, 0xB2, 0x20, 0x64, 0x2F, 0x45, 0x1C, 0xE4, 0x1F, 0xDD,
	        0x54, 0x18, 0xBE, 0x5C, 0x40, 0x7A, 0x20, 0x23, 0x1D, 0x95, 0x56, 0x85,
	        0x6B, 0x00, 0x08, 0xB6, 0x86, 0xC5, 0x56, 0xAB, 0xE1, 0x6A, 0xAF, 0x67,
	        0x5E, 0x29, 0x9B, 0x22, 0x95, 0x49, 0x7C, 0x2A, 0x7F, 0x4D, 0x07, 0x07,
	        0xA2, 0x1C, 0xCC, 0x5E, 0x00, 0xAE, 0xEC, 0xF2, 0xC0, 0x44, 0xA6, 0x8A,
	        0xDF, 0x8A, 0xD9, 0x83, 0xAA, 0xF2, 0x49, 0x05, 0x4A, 0x90, 0x61, 0x57,
	        0xB4, 0x57, 0xC1, 0x93, 0xB7, 0x71, 0x27, 0x1E, 0xB7, 0x68, 0xFF, 0x44,
	        0x67, 0xDD, 0x57, 0x2B, 0x81, 0x73, 0x12, 0x23, 0x32, 0x1A, 0x3D, 0x0D,
	        0x11, 0x47, 0x2F, 0x87, 0x15, 0x0F, 0xBD, 0x8F, 0x5F, 0xE0, 0x14, 0x2E,
	        0xBE, 0xB3, 0xF4, 0x43, 0xBF, 0x96, 0x7E, 0x7E, 0xE8, 0x0B, 0xCE, 0xE7,
	        0xA1, 0x62, 0xBC, 0x6D, 0x71, 0x88, 0xA9, 0x90, 0x9B, 0x18, 0xB7, 0x66,
	        0x20, 0x97, 0xC0, 0x68, 0xBA, 0x20, 0x5A, 0xBD, 0x21, 0x3A, 0xBB, 0xE1,
	        0xD2, 0x8B, 0xD2, 0x3B, 0xF1, 0x18, 0xB3, 0xC3, 0x08, 0xC7, 0xD4, 0x3A,
	        0xCD, 0xE8, 0x6F, 0x4D, 0x67, 0x29, 0x5B, 0x80, 0x41, 0x43, 0x2C, 0x82,
	        0xA7, 0xDC, 0x07, 0x96, 0xC7, 0x96, 0x0B, 0x2E, 0x99, 0xC8, 0x08, 0x5A,
	        0xB2, 0xC6, 0xEF, 0xCE, 0xD0, 0x42, 0xE5, 0x4B, 0x2B, 0x61, 0x25, 0x98,
	        0x7C, 0x30, 0x40, 0x6B, 0xD8, 0x39, 0x1E, 0xC8, 0xF9, 0x15, 0x5C, 0x21,
	        0xF9, 0x92, 0xCE, 0x69, 0xA6, 0xD3, 0xFA, 0x65, 0xCF, 0x8D, 0xD3, 0x5E,
	        0x23, 0xBC, 0x16, 0x1D, 0x7A, 0xEF, 0x69, 0xA9, 0x4E, 0x77, 0x61, 0x6D,
	        0x64, 0x67, 0x73, 0x33, 0x50, 0x6B, 0x5A, 0x08, 0x00, 0x06, 0x00, 0x01,
	        0x00, 0x01, 0x00, 0x01, 0x4D, 0xCB, 0x68, 0x8D, 0xB5, 0xB7, 0x2B, 0x77,
	        0xB3, 0x37, 0x70, 0x94, 0x2C, 0x4B, 0x58, 0xE4, 0xF0, 0x20, 0xAC, 0xB7,
	        0xCE, 0xAF, 0xB9, 0xA1, 0x9E, 0x98, 0x6F, 0x1F, 0xDF, 0xED, 0x30, 0x2C,
	        0xB8, 0x98, 0xBA, 0x6D, 0x4F, 0xE5, 0xE4, 0x02, 0x1D, 0x55, 0x96, 0x49,
	        0xA0, 0xEF, 0x2D, 0x90, 0x0F, 0xD9, 0x47, 0xDE, 0x88, 0x01, 0x7F, 0x26,
	        0x99, 0xEE, 0x45, 0x78, 0xDB, 0x76, 0xA5, 0x93, 0x0E, 0x5F, 0x01, 0x5E,
	        0x44, 0xE5, 0x60, 0x61, 0x3A, 0xF7, 0xAA, 0xDB, 0xE6, 0xD7, 0x6B, 0xF5,
	        0xB0, 0xB3, 0xD3, 0x38, 0xB4, 0x52, 0x5E, 0x4D, 0x41, 0xA4, 0x6A, 0x61,
	        0x63, 0x6B, 0x6A, 0x73, 0x6F, 0x6E, 0x01, 0x0D, 0x00, 0x00, 0x7D, 0x47,
	        0x00, 0x00, 0x00
        };

    }
}
